<?php
  class XunfeiTTS {
    function tocurl($url, $header, $content){
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
        $response = curl_exec($ch);
        $error=curl_error($ch);
        //var_dump($error);
        if($error){
            die($error);
        }
        $header = curl_getinfo($ch);

        curl_close($ch);
        $data = array('header' => $header,'body' => $response);
        return $data;
    }
    function xfyun($str) {
        $url = 'http://api.xfyun.cn/v1/service/v1/tts';
        $appid = '5c09c800';
        $apikey = '8fc0e1871df446d2f5653b0e7ae0d7e7';
        $param = array (
            'aue' => 'lame',
            'voice_name' => 'xiaoyan',
        );
        $time = (string)time();
        $xparam = base64_encode(json_encode(($param)));
        $checksum = md5($apikey.$time.$xparam);
        $header = array(
            'X-CurTime:'.$time,
            'X-Param:'.$xparam,
            'X-Appid:'.$appid,
            'X-CheckSum:'.$checksum,
            'X-Real-Ip:115.159.192.38',
            'Content-Type:application/x-www-form-urlencoded; charset=utf-8'
        );
        $content = array(
            'text' => $str,
        );
        $response = $this->tocurl($url, $header, $content);
        $header = $response['header'];
        if($header['content_type'] == 'audio/mpeg'){
            $filename.= 'audio/'.$time.'.mp3';
            $res = file_put_contents($filename, $response['body']);
            return $filename;
        }else{
            $filename.= 'audio/'.$time.'.wav';
            $res = file_put_contents($filename, $response['body']);
            return $filename;
        }

    }
}
?>
